package cn.tomoki.demo01.controller;

import cn.tomoki.demo01.entity.Product;
import cn.tomoki.demo01.entity.ProductAppointOverview;
import cn.tomoki.demo01.mapper.ProductMapper;
import cn.tomoki.demo01.service.ProductServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;

@RestController
public class ProductController {
    @Autowired
    public ProductServiceImpl productService;
    ObjectMapper mapper = new ObjectMapper();

    @GetMapping("/getProductAll")
    public String getProductAll(){
        Collection<Product> products = productService.getProductAll();
        try {
            return mapper.writeValueAsString(products);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/getProductById")
    public String getProductById(String id){
        Product product = productService.getProductById(id);
        try {
            return mapper.writeValueAsString(product);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
    @GetMapping("/getProductAppointOverview")
    public String  getProductAppointOverview(){
        Collection<ProductAppointOverview> productAppointOverview = productService.getProductAppointOverview();
        try {
            return mapper.writeValueAsString(productAppointOverview);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
